<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-controller
      class="tab-controller"
      :titles="['新款', '流行', '精选']"
      @tabClick="tabClick"
      ref="tabController2" v-show="tabControllerShow"></tab-controller>
    <scroll
      class="content"
      ref="scroll"
      :probeType="3"
      @scroll="homeContentScroll"
      :pullUpLoad="true"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" ref="hSwiper"></home-swiper>
      <recommend :recommends="recommends"></recommend>
      <feature></feature>
      <tab-controller
        :titles="['新款', '流行', '精选']"
        @tabClick="tabClick"
        ref="tabController"
      ></tab-controller>
      <good-list :goods="goods[currentPage]"></good-list>
    </scroll>

    <back-top @click.native="backClick" v-show="currentScroll"></back-top>
  </div>
</template>
<script>
// 公共组件
import NavBar from "components/common/navbar/NavBar";
import TabController from "components/content/TabController";
import GoodList from "components/content/goods/GoodList";
import Scroll from "components/common/scroll/Scroll";
//私有组件
import HomeSwiper from "./childComps/HomeSwiper";
import Recommend from "./childComps/Recommend";
import Feature from "./childComps/Feature";

//方法
import { getHomeMultidata, getHomeGoods } from "network/Home";
import {mixins,backTopMixin} from "assets/common/mixins"
export default {
  name: "Home",
  components: {
    NavBar,
    TabController,
    GoodList,
    Scroll,

    HomeSwiper,
    Recommend,
    Feature
  },
  data() {
    return {
      banners: [], //首页轮播图数据
      recommends: [], //分类数据
      //商品数据
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] }
      },
      currentPage: "pop",
      tabControllerShow:false,//tabContrller吸顶效果
      tabScroll:0,
      scrollY:0,//路由切换时记录当前滚动的Y轴坐标
    };
  },
  mixins:[mixins,backTopMixin],
  created() {
    this.getHomeMultidata(),
      //商品数据
      this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  updated(){
    this.tabScroll=this.$refs.tabController.$el.offsetTop;
  },
  activated: function() {
    this.$refs.hSwiper.startTimer();
    /**Home路由活跃时，better-scroll滚动到指定位置 */   
    this.$refs.scroll.scrollTo(0,this.scrollY,0);
    this.$refs.scroll.refresh();
    
  },
  deactivated: function() {
    this.$refs.hSwiper.stopTimer();
    this.scrollY=this.$refs.scroll.getScrollY();
    

    /**离开时取消bus监听 */
    this.$bus.$off('imgLoad',this.imgLoadListener);
  },

  methods: {
    /**
     * 子传父事件
     */
    loadMore() {
      //加载更多图片
      this.getHomeGoods(this.currentPage);
      this.$refs.scroll.refresh();
    },

    homeContentScroll(position) {
      this.currentScroll = -position.y > 1000 ;
      this.tabControllerShow=-(position.y)>this.tabScroll;
    },

    tabClick(index) {
      switch (index) {
        case 0:
          this.currentPage = "pop";
          break;
        case 1:
          this.currentPage = "new";
          break;
        case 2:
          this.currentPage = "sell";
          break;
      }
      /**两个tabController点击同步 */
      this.$refs.tabController.currentIndex=index;
      this.$refs.tabController2.currentIndex=index;
    },


    /**
     * 网络请求
     */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },

    getHomeGoods(type) {
      let page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page++;
        this.$refs.scroll.finishPullUp(); //告诉better-scroll上拉加载数据已完毕
      });
    }
  }
};
</script>
<style lang="less" scoped>
#home {
  position: relative;
  height: 100vh;
  width: 100%;
}
.home-nav {
  background: var(--color-tint);
  color: white;
}
.tab-controller {
  position: relative;
  top: -1px;
  left: 0;
  right: 0;
  z-index: 9;
  background-color: #fff;
}
.content {
  position: absolute;
  top: 43px;
  bottom: 49px;
  overflow: hidden;
}
</style>
